@echo off
echo 正在测试Java Nacos客户端与Rust Nacos服务器的兼容性...
echo.

REM 检查Java是否安装
java -version >nul 2>&1
if errorlevel 1 (
    echo 错误：Java未安装，请先安装Java
    pause
    exit /b 1
)

echo ✅ Java已安装

REM 创建临时测试目录
set TEST_DIR=%TEMP%\nacos-java-test
if exist "%TEST_DIR%" rmdir /s /q "%TEST_DIR%"
mkdir "%TEST_DIR%"
cd /d "%TEST_DIR%"

echo 📁 创建测试目录: %TEST_DIR%

REM 创建Maven项目结构
mkdir src\main\java\com\example\nacos\test
mkdir src\main\resources

REM 创建pom.xml文件
echo ^<?xml version="1.0" encoding="UTF-8"?^> > pom.xml
echo ^<project xmlns="http://maven.apache.org/POM/4.0.0"^> >> pom.xml
echo     ^<modelVersion^>4.0.0^</modelVersion^> >> pom.xml
echo     ^<groupId^>com.example^</groupId^> >> pom.xml
echo     ^<artifactId^>nacos-rust-test^</artifactId^> >> pom.xml
echo     ^<version^>1.0.0^</version^> >> pom.xml
echo     ^<properties^> >> pom.xml
echo         ^<maven.compiler.source^>8^</maven.compiler.source^> >> pom.xml
echo         ^<maven.compiler.target^>8^</maven.compiler.target^> >> pom.xml
echo     ^</properties^> >> pom.xml
echo     ^<dependencies^> >> pom.xml
echo         ^<dependency^> >> pom.xml
echo             ^<groupId^>com.alibaba.nacos^</groupId^> >> pom.xml
echo             ^<artifactId^>nacos-client^</artifactId^> >> pom.xml
echo             ^<version^>2.3.0^</version^> >> pom.xml
echo         ^</dependency^> >> pom.xml
echo     ^</dependencies^> >> pom.xml
echo ^</project^> >> pom.xml

echo ✅ 创建Maven配置文件

REM 创建Java测试类
echo package com.example.nacos.test; > src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.NacosFactory; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.config.ConfigService; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.config.listener.Listener; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.exception.NacosException; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.naming.NamingService; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo import com.alibaba.nacos.api.naming.pojo.Instance; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo public class NacosCompatibilityTest { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     private static final String SERVER_ADDR = "127.0.0.1:8848"; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     public static void main(String[] args) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("🧪 开始测试Rust Nacos服务器兼容性..."); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         try { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             testNamingService(); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             testConfigService(); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             System.out.println("✅ 所有兼容性测试通过！"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } catch (Exception e) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             System.err.println("❌ 测试失败: " + e.getMessage()); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             e.printStackTrace(); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             System.exit(1); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     private static void testNamingService() throws Exception { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("🔍 测试命名服务..."); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         NamingService naming = NacosFactory.createNamingService(SERVER_ADDR); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 注册服务实例 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         Instance instance = new Instance(); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         instance.setIp("127.0.0.1"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         instance.setPort(8080); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         instance.setServiceName("rust-test-service"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         instance.setHealthy(true); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         naming.registerInstance("rust-test-service", instance); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 服务实例注册成功"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 查询服务实例 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         List<Instance> instances = naming.getAllInstances("rust-test-service"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         if (instances.isEmpty()) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             throw new RuntimeException("服务实例查询失败"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 服务实例查询成功，发现 " + instances.size() + " 个实例"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 注销服务实例 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         naming.deregisterInstance("rust-test-service", "127.0.0.1", 8080); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 服务实例注销成功"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     private static void testConfigService() throws Exception { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("⚙️ 测试配置服务..."); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         ConfigService config = NacosFactory.createConfigService(SERVER_ADDR); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         String dataId = "rust-test-config"; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         String group = "DEFAULT_GROUP"; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         String content = "test-content-from-java-client"; >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 发布配置 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         boolean publishResult = config.publishConfig(dataId, group, content); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         if (!publishResult) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             throw new RuntimeException("配置发布失败"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 配置发布成功"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 获取配置 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         String retrievedContent = config.getConfig(dataId, group, 3000); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         if (!content.equals(retrievedContent)) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             throw new RuntimeException("配置内容不一致"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 配置获取成功"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo. >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         // 删除配置 >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         boolean removeResult = config.removeConfig(dataId, group); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         if (!removeResult) { >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo             throw new RuntimeException("配置删除失败"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo         System.out.println("✅ 配置删除成功"); >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo     } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java
echo } >> src\main\java\com\example\nacos\test\NacosCompatibilityTest.java

echo ✅ 创建Java测试类

REM 编译并运行测试
echo 🏗️ 正在编译Java测试代码... >> "%TEST_DIR%\compile.log" 2>&1

REM 运行Maven测试
echo 🚀 运行Java兼容性测试...
call mvn compile exec:java -Dexec.mainClass="com.example.nacos.test.NacosCompatibilityTest" -Dexec.classpathScope=test

if errorlevel 1 (
    echo ❌ Java兼容性测试失败
    echo 请检查Rust服务器是否正在运行，或手动运行测试
    echo 测试目录: %TEST_DIR%
) else (
    echo ✅ Java兼容性测试通过！
)

pause